/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.commons;

import java.io.Serializable;
import javax.swing.JTable;
import org.balisunrise.common.EntityList;
import org.balisunrise.common.Grid;
import org.balisunrise.common.EntityLoader;
import org.balisunrise.common.VisualEntityType;
import org.balisunrise.common.abstracts.AbstractVisualEntity;
import org.balisunrise.swing.components.panel.GridPanel;

/**
 *
 * @author Glauco
 */
public class EntityGrid extends AbstractVisualEntity
        implements org.balisunrise.common.EntityGrid<GridPanel>{

    public static final String PROP_GRID = "grid";
    public static final String PROP_ENTITY_LOADER = "entityLoader";
    public static final String PROP_VISUAL_REPRESENTATION =
            "visualRepresentation";

    private Grid<JTable> grid;
    private EntityLoader entityLoader;
    private GridPanel visualRepresentation;

    public void setVisualRepresentation(GridPanel visualRepresentation) {
        GridPanel old = this.visualRepresentation;
        this.visualRepresentation = visualRepresentation;
        changeSupport.firePropertyChange(PROP_VISUAL_REPRESENTATION, old,
                visualRepresentation);
    }

    public void setGrid(Grid grid) {
        Grid old = this.grid;
        this.grid = grid;
        changeSupport.firePropertyChange(PROP_GRID, old, grid);
    }

    @Override
    public Grid getGrid() {
        return grid;
    }

    @Override
    public void setEntityLoader(EntityLoader loader) {
        EntityLoader old = this.entityLoader;
        if (loader != null && grid != null){
            GridModel gm = (GridModel) grid.getGridModel();
            if(gm != null)
                gm.setEntityList(loader.getEntityList());
        }
        this.entityLoader = loader;
        changeSupport.firePropertyChange(PROP_ENTITY_LOADER, old, entityClass);
    }

    @Override
    public EntityLoader getEntityLoader() {
        return entityLoader;
    }

    @Override
    public Serializable getSelectedEntity() {
        if(grid == null)
            return null;

        int index = grid.getVisualRepresentation().getSelectedRow();
        return entityLoader.getEntityList().getEntityAt(index);
    }

    @Override
    public GridPanel getVisualRepresentation() {
        return visualRepresentation;
    }

    /**
     * Retorna o tipo do VisualEntity.<BR>
     * Sempre retornará ENTITY_GRID.
     * @return VisualEntityType.ENTITY_GRID
     */
    @Override
    public VisualEntityType getVisualEntityType() {
        return VisualEntityType.ENTITY_GRID;
    }

    @Override
    public String getName() {
        return getVisualRepresentation().getName();
    }

    @Override
    public EntityList getEntityList() {
        if(grid == null)
            return null;

        if(grid.getGridModel() == null)
            return null;

        return grid.getGridModel().getEntityList();
    }
}
